package cn.yiya.shiji.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import cn.yiya.shiji.R;
import cn.yiya.shiji.business.ApiRequest;
import cn.yiya.shiji.business.HttpMessage;
import cn.yiya.shiji.business.MsgCallBack;
import cn.yiya.shiji.business.RetrofitRequest;
import cn.yiya.shiji.config.BaseApplication;
import cn.yiya.shiji.config.Configration;
import cn.yiya.shiji.config.CustomEvent;
import cn.yiya.shiji.entity.CarCountInfo;
import cn.yiya.shiji.entity.VersionData;
import cn.yiya.shiji.fragment.NewClassifyFragment;
import cn.yiya.shiji.fragment.NewDiscoverFragment;
import cn.yiya.shiji.fragment.NewMainHomeFragment;
import cn.yiya.shiji.fragment.PersonalCenterFragment;
import cn.yiya.shiji.fragment.TravleNavigationFragment;
import cn.yiya.shiji.utils.MyPreference;
import cn.yiya.shiji.utils.NetUtil;
import cn.yiya.shiji.views.ProgressDialog;
import cn.yiya.shiji.views.ThemeManager;

/**
 * Created by chenjian on 2016/5/26.
 */
public class NewMainActivity extends BaseAppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener{

    public static final String MALL = "MALL";
    public static final String CLASSIFY = "CLASSIFY";
    public static final String NAVIGATION = "NAVIGATION";
    public static final String FIND = "FIND";
    public static final String MINE = "MINE";

    private FragmentManager fm;
    private String FragmentTag = MALL;
    private Fragment mFragment;
    private Handler mHandler;


    private String mainActivityDest = "";

    public static boolean isActive = false;
    Toolbar toolbar;

    private ImageView ivMiddle;        // 中间图标
    private TextView tvMiddle;          // 中间文字
    private LinearLayout llytFind;      // 发现布局
    private RelativeLayout rlytSearch;  // 分类搜索
    public TextView tvCarcount;         // 购物车数量
    private ImageView ivLeft;           // 左边图标
    private RelativeLayout rlytCar;     // 右边购物车布局

    private LinearLayout llytTips;      // 引导提示
    private LinearLayout llytTipsWork;

    private int nLeftType;
    private static final int SEARCH = 111;
    private static final int CAMERA = 222;
    private static final int SETTING = 333;

    private final static int CAR_REQUEST_CODE = 1000;
    private static final int REQUEST_SETTINGS = 1001;
    public static boolean bLogin;

    public TextView tvFastTop;

    public int dapeiLength=0;
    private boolean bDapeiMore=false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_activity_main);

        ThemeManager.init(this, 2, 0, null);

        initIntent();

        initViews();
        initFragment(savedInstanceState);
        initEvents();
        init();
    }


    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ivLeft = (ImageView) findViewById(R.id.toolbar_left);

        ivMiddle = (ImageView) findViewById(R.id.toolbar_middle_img);
        tvMiddle = (TextView) findViewById(R.id.toolbar_middle_txt);
        rlytSearch = (RelativeLayout) findViewById(R.id.toolbar_middle_search_layout);
        llytFind = (LinearLayout) findViewById(R.id.toolbar_middle_layout);

        tvCarcount = (TextView) findViewById(R.id.toolbar_right_count);
        rlytCar = (RelativeLayout) findViewById(R.id.toolbar_right_layout);

        rlytCar.setVisibility(View.VISIBLE);

        llytTips = (LinearLayout) findViewById(R.id.llyt_tips_travel);
        llytTipsWork = (LinearLayout) findViewById(R.id.llyt_tips_work);

        tvFastTop = (TextView) findViewById(R.id.tv_fast_top_guide_tips);
    }

    @Override
    protected void initEvents() {
        ((RadioGroup) findViewById(R.id.rdgroup_main)).setOnCheckedChangeListener(this);
        rlytCar.setOnClickListener(this);
        ivLeft.setOnClickListener(this);
    }

    @Override
    protected void init() {
        mHandler = new Handler(getMainLooper());
        checkNewSkin();
        initUpdateVersion();
//        initCarCount();
    }

    private void initCarCount() {
        tvCarcount.setVisibility(View.GONE);
        new RetrofitRequest<CarCountInfo>(ApiRequest.getApiShiji().getCarCount()).handRequest(new MsgCallBack() {
            @Override
            public void onResult(HttpMessage msg) {
                if (msg.isSuccess()) {
                    bLogin = true;
                    tvCarcount.setVisibility(View.VISIBLE);
                    CarCountInfo info = (CarCountInfo) msg.obj;
                    tvCarcount.setText("" + info.getCount());
                    if (info.getCount() == 0) {
                        tvCarcount.setVisibility(View.GONE);
                    }

                } else if (msg.isLossLogin()) {
                    tvCarcount.setVisibility(View.GONE);
                    bLogin = false;
                }
            }
        });
    }

    // 设置导航的toolbar
    private void setNavigationToolbar() {
        ivLeft.setVisibility(View.GONE);
        ivMiddle.setVisibility(View.GONE);
        rlytSearch.setVisibility(View.GONE);
        llytFind.setVisibility(View.GONE);

        tvMiddle.setVisibility(View.VISIBLE);
        tvMiddle.setText("目的地");
    }

    // 设置商城的toolbar
    private void setMallToolbar() {
        rlytSearch.setVisibility(View.GONE);
        llytFind.setVisibility(View.GONE);
        tvMiddle.setVisibility(View.GONE);

        ivLeft.setVisibility(View.VISIBLE);
        ivLeft.setImageResource(R.mipmap.icon_new_search);
        ivMiddle.setVisibility(View.VISIBLE);
        nLeftType = SEARCH;
    }

    // 设置分类的toolbar
    private void setClassifyToolbar() {
        ivLeft.setVisibility(View.GONE);
        ivMiddle.setVisibility(View.GONE);
        llytFind.setVisibility(View.GONE);
        tvMiddle.setVisibility(View.GONE);

        rlytSearch.setVisibility(View.VISIBLE);
    }

    // 设置发现的toolbar
    private void setFindToolbar() {
        tvMiddle.setVisibility(View.GONE);
        ivMiddle.setVisibility(View.GONE);
        rlytSearch.setVisibility(View.GONE);

        ivLeft.setVisibility(View.VISIBLE);
        ivLeft.setImageResource(R.mipmap.ic_camera);
        llytFind.setVisibility(View.VISIBLE);
        nLeftType = CAMERA;

        addGuide(llytTipsWork, MyPreference.MAIN_WORK_GUIDE );
    }

    // 设置个人中心的toolbar
    private void setPersonToolbar() {
        ivMiddle.setVisibility(View.GONE);
        rlytSearch.setVisibility(View.GONE);
        llytFind.setVisibility(View.GONE);

        ivLeft.setVisibility(View.VISIBLE);
        ivLeft.setImageResource(R.mipmap.shezhi);
        tvMiddle.setVisibility(View.VISIBLE);
        tvMiddle.setText("我");
        nLeftType = SETTING;
    }

    private void initIntent() {
        Intent intent = getIntent();
        if(intent != null) {
            mainActivityDest = intent.getStringExtra("main_activity_dest");
            if (!TextUtils.isEmpty(mainActivityDest)) {
                String flashId = intent.getStringExtra("flashId");
                if (mainActivityDest.equals("flash")) {
                    mainActivityDest = "";
                    intent = new Intent(this, HomeIssueActivity.class);
                    intent.putExtra("type", Configration.FLASH_SALE);
                    intent.putExtra("flashId", flashId);
                    startActivity(intent);
                }
            }
        }
    }

    /**
     * 解决fragment重叠问题
     * @param bundle
     */
    private void initFragment(Bundle bundle) {
        fm = getSupportFragmentManager();
        if (bundle != null) {
            String tag = bundle.getString("TAG");
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment;
            String[] strArr = new String[]{MALL, CLASSIFY, NAVIGATION, FIND, MINE};
            for (String str : strArr) {
                fragment = fm.findFragmentByTag(str);
                if (fragment != null) {
                    ft.hide(fragment);
                }
            }
            ft.commit();
            if (TextUtils.isEmpty(tag)) {
                setMallToolbar();
                turnPage(MALL);
            } else {
                turnPage(tag);
            }
        } else {
            setMallToolbar();
            turnPage(MALL);
        }
    }

    //fragment 跳转
    public void turnPage(String tag) {
        FragmentTransaction ft = fm.beginTransaction();
        FragmentTag = tag;
        //隐藏当前显示的fragment
        if (mFragment != null) {
            ft.hide(mFragment);
        }
        //获取要显示的fragment
        mFragment = fm.findFragmentByTag(tag);
        if (mFragment == null) {
            mFragment = creatFragmentByTag(tag);
            if(mFragment != null){
                ft.add(R.id.container_main, mFragment, tag);
            }
        }
        //显示
        ft.show(mFragment).commit();
    }

    //fragment 跳转 查看更多跳转发现推荐搭配
    public void turnPage(String tag,boolean bDapeiMore,int position) {
        FragmentTransaction ft = fm.beginTransaction();
        FragmentTag = tag;
        //隐藏当前显示的fragment
        if (mFragment != null) {
            ft.hide(mFragment);
        }
        //获取要显示的fragment
        mFragment = fm.findFragmentByTag(tag);
        if (mFragment == null) {
            mFragment = creatFragmentByTag(tag);
            if(mFragment != null){
                ft.add(R.id.container_main, mFragment, tag);
            }
        }

        if(mFragment instanceof NewDiscoverFragment){
            ((NewDiscoverFragment)mFragment).setDapeiMore(bDapeiMore,position);
        }
        //显示
        ft.show(mFragment).commit();

    }

    // 需要显示的Fragment
    public Fragment creatFragmentByTag(String tag) {
        if(tag == null){
            return null;
        }

        Fragment fragment;
        switch (tag) {
            case MALL:          // 商城
                fragment = new NewMainHomeFragment();
                ((NewMainHomeFragment)fragment).setCustomlistener(new NewMainHomeFragment.CustomActionListener() {
                    @Override
                    public void goToDiscover(int position) {
                         dapeiLength=position;//搭配列表数量
                         bDapeiMore=true;
                        ((RadioButton)findViewById(R.id.rd_main_find)).setChecked(true);
                    }
                });
                break;
            case CLASSIFY:      // 分类
                fragment = new NewClassifyFragment();
                break;
            case FIND:          // 发现
                fragment = new NewDiscoverFragment();
                break;
            case NAVIGATION:    // 导航
                fragment = new TravleNavigationFragment();
                break;
            case MINE:          // 我
                fragment = new PersonalCenterFragment();
                Bundle bundle= new Bundle();
                bundle.putBoolean("isCurUser",true);
                fragment.setArguments(bundle);
                break;
            default:
                fragment = new NewMainHomeFragment();
                break;
        }
        return fragment;
    }

    /**
     * 检查服务器是否有新的皮肤更新
     */
    private void checkNewSkin() {
        SharedPreferences sharedPreferences = getSharedPreferences("skin", 0);
        String endTime = sharedPreferences.getString("endtime", "");
        String nowTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());

        try {
            Date endDate = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(endTime);               // 活动结束时间
            Date nowDate = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(nowTime);
            if (nowDate.compareTo(endDate) <= 0) {
                setBitmap(R.id.rd_main_home, "7", "2");
                setBitmap(R.id.rd_main_classify, "8", "3");
                setBitmap(R.id.rd_main_navigation, "9", "4");
                setBitmap(R.id.rd_main_find, "10", "5");
                setBitmap(R.id.rd_main_mine, "11", "6");
            }

        } catch (Exception e) {}
    }

    /**
     * 将新的皮肤图片设置进去
     * @param id
     */
    private void setBitmap(int id, String newPath, String oldPath) {
        File skinFile = new File(SplashActivity.SKIN_PATH);
        if (!skinFile.exists()) {
            return;
        }
        Drawable betrue = new BitmapDrawable(getResources(), BitmapFactory.decodeFile((SplashActivity.SKIN_PATH + newPath + ".png")));
        Drawable befalse = new BitmapDrawable(getResources(), BitmapFactory.decodeFile((SplashActivity.SKIN_PATH + oldPath + ".png")));

        if (betrue != null && befalse != null) {
            StateListDrawable stateListDrawable = new StateListDrawable();
            stateListDrawable.addState(new int[]{android.R.attr.state_checked}, betrue);
            stateListDrawable.addState(new int[]{-android.R.attr.state_checked}, befalse);
            RadioButton rb = (RadioButton) findViewById(id);
            rb.setTextAppearance(this, R.style.main_radio_group);
            rb.setCompoundDrawablesWithIntrinsicBounds(null, stateListDrawable, null, null);
        }
    }

    // 判断当前是否有更新
    private void initUpdateVersion() {
        if (!BaseApplication.bUpdate) {
            final int localVerson = BaseApplication.getInstance().getCurVersion();
            new RetrofitRequest<VersionData>(ApiRequest.getApiShiji().getVersion(String.valueOf(localVerson)))
                    .handRequest(new MsgCallBack() {
                        @Override
                        public void onResult(HttpMessage msg) {
                            if (msg.isSuccess() && msg.obj != null) {
                                VersionData remoteVer = (VersionData) msg.obj;
                                int remoteVersion = Integer.parseInt(remoteVer.getAndroid().getCode());
                                if (remoteVersion > localVerson) {
                                    ProgressDialog.showUpdateVersionDialog(NewMainActivity.this, remoteVer, 1);
                                } else {
                                    BaseApplication.bUpdate = false;
                                }
                            }
                        }
                    });
        }

    }

    @Override
    protected void onResume() {
        isActive = true;
        super.onResume();

         // 跳到首页
        Intent intent = getIntent();
        if (intent != null) {
            String fragId = intent.getStringExtra("fragid");
            if (!TextUtils.isEmpty(fragId) && fragId.equals(MALL)) {
                ((RadioButton) findViewById(R.id.rd_main_home)).setChecked(true);
                intent.putExtra("fragid", "");
            }
        }

        initCarCount();

        // 添加目的地按钮引导
        addGuide(llytTips, MyPreference.MAIN_TRAVEL_GUIDE);
//        if (!TextUtils.isEmpty(intent.getStringExtra("order"))) {
//            if (intent.getStringExtra("order").equals("order")) {
//                RadioButton rb = (RadioButton) findViewById(R.id.rd_main_home);
//                rb.setChecked(true);
//                intent.putExtra("order", "");
//            } else if(intent.getStringExtra("order").equals("shoppingCar")){
//                RadioButton rb = (RadioButton) findViewById(R.id.rd_main_shop);
//                rb.setChecked(true);
//                intent.putExtra("order", "");
//            }
//        }
//        isActive = true;
//        super.onResume();
//
//        if (BaseApplication.bRefuse) {
//            finish();
//            System.exit(0);
//            Process.killProcess(Process.myPid());
//        }
    }


    @Override
    protected void onPause() {
        isActive = false;
        super.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //保存当前显示的fragment名
        outState.putString("TAG", FragmentTag);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rd_main_find:      // 发现
                CustomEvent.onEvent(NewMainActivity.this, ((BaseApplication) getApplication()).getDefaultTracker(),
                        "MainActivity", CustomEvent.SwitchDiscover);
                setFindToolbar();
                if(!bDapeiMore) {
                    dapeiLength=0;
                }
                turnPage(FIND,bDapeiMore,dapeiLength);
                bDapeiMore=false;
                break;
            case R.id.rd_main_classify:  // 分类
                CustomEvent.onEvent(NewMainActivity.this, ((BaseApplication) getApplication()).getDefaultTracker(),
                        "MainActivity", CustomEvent.SwitchCategory);
                setClassifyToolbar();
                turnPage(CLASSIFY);
                break;
            case R.id.rd_main_home:     // 商城
                CustomEvent.onEvent(NewMainActivity.this, ((BaseApplication) getApplication()).getDefaultTracker(),
                        "MainActivity", CustomEvent.SwitchHomePage);
                setMallToolbar();
                turnPage(MALL);
                break;
            case R.id.rd_main_mine:     // 我
                CustomEvent.onEvent(NewMainActivity.this, ((BaseApplication) getApplication()).getDefaultTracker(),
                        "MainActivity", CustomEvent.SwitchMine);
                setPersonToolbar();
                turnPage(MINE);
                break;
            case R.id.rd_main_navigation:         // 导航
                CustomEvent.onEvent(NewMainActivity.this, ((BaseApplication) getApplication()).getDefaultTracker(),
                        "MainActivity", CustomEvent.SwitchDiscover);
                setNavigationToolbar();
                turnPage(NAVIGATION);
                break;
        }
    }

    /**
     * 连续按返回键退出标志
     */
    private boolean isExit = false;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

           if (mFragment instanceof NewDiscoverFragment) {
                boolean bHide = ((NewDiscoverFragment) mFragment).hidePopupWindow();
                if (bHide) {
                    return true;
                }
            }
            RadioButton rd = (RadioButton) findViewById(R.id.rd_main_home);
            if (!rd.isChecked()) {
                    rd.setChecked(true);
                return true;
            } else {
                if (isExit) {
                    finish();
                    BaseApplication.getInstance().fastTopGuide = false;
                    return super.onKeyDown(keyCode, event);
                } else {
                    isExit = true;
                    showTips("再按一次将退出应用！");
                    //3秒后重置
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            isExit = false;
                        }
                    }, 3000);
                    return true;
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            // 当前从登陆界面回来，并且停留在个人中心界面，刷新
            if (data != null) {
                if (data.getBooleanExtra("success", false)) {
                    if (mFragment instanceof PersonalCenterFragment) {
                        ((PersonalCenterFragment) mFragment).setLogIn();
                    }
                }
            }
            switch (requestCode) {
                case CAR_REQUEST_CODE:
                    break;
                case REQUEST_SETTINGS:
                    if (mFragment instanceof PersonalCenterFragment) {
                        ((PersonalCenterFragment) mFragment).setLogOut();
                    }
                    break;
            }
        }
        else if (resultCode == RESULT_CANCELED) {

        }
    }

    public void showTips(String msg){
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    // 添加快速回到顶部的提示
    public void addBackTopGuide(){
        if (!BaseApplication.getInstance().fastTopGuide) {
            BaseApplication.getInstance().fastTopGuide = true;
//            AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
            TranslateAnimation alphaAnimation = new TranslateAnimation(-1200 ,0, 0, 0);
            alphaAnimation.setFillAfter(true);
            alphaAnimation.setDuration(1000);
            alphaAnimation.setStartOffset(500);
            tvFastTop.setVisibility(View.VISIBLE);
            tvFastTop.setAnimation(alphaAnimation);
            alphaAnimation.startNow();
            final Handler mHandler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what) {
                        case 10:
                            if(tvFastTop.getVisibility() == View.VISIBLE) {
                                TranslateAnimation alphaAnimation = new TranslateAnimation(0 ,-1200, 0, 0);
                                alphaAnimation.setFillAfter(true);
                                alphaAnimation.setDuration(1000);
                                tvFastTop.setVisibility(View.INVISIBLE);
                                tvFastTop.setAnimation(alphaAnimation);
                                alphaAnimation.startNow();
                            }
                            break;
                    }
                }
            };

            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run() {
                    mHandler.sendEmptyMessage(10);
                }
            }, 4000);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_right_layout:
                if(isEffectClick()) {
                    goToCarView();
                }
                break;
            case R.id.toolbar_left:
                switch (nLeftType) {
                    case SEARCH:
                        Intent intent = new Intent(this, NewSearchActivity.class);
                        startActivity(intent);
                        break;
                    case CAMERA:
                        if (mFragment instanceof NewDiscoverFragment) {
                           //到NewDiscoverFragment的方法中执行
                            if(bLogin) {
                                ((NewDiscoverFragment) mFragment).ClickCamera();
                            }else{
                                Intent intentLogin = new Intent(NewMainActivity.this, LoginActivity.class);
                                startActivityForResult(intentLogin, REQUEST_SETTINGS);
                                overridePendingTransition(R.anim.slide_in_top_buttom, R.anim.slide_out_top_buttom);
                            }
                        }
                        break;
                    case SETTING:
                        if(isEffectClick()) {
                            goToSetting();
                        }
                        break;
                }
                break;
        }
    }

    private void goToSetting() {
        new RetrofitRequest<>(ApiRequest.getApiShiji().isLogin()).handRequest(new MsgCallBack() {
            @Override
            public void onResult(HttpMessage msg) {
                if (msg.isSuccess()) {
                    Intent intent0 = new Intent(NewMainActivity.this, SettingActivity.class);
                    startActivityForResult(intent0, REQUEST_SETTINGS);
                } else if (msg.isLossLogin()) {
                    Intent intent = new Intent(NewMainActivity.this, LoginActivity.class);
                    startActivityForResult(intent, REQUEST_SETTINGS);
                    overridePendingTransition(R.anim.slide_in_top_buttom, R.anim.slide_out_top_buttom);
                } else {
                    if(!NetUtil.IsInNetwork(NewMainActivity.this)){
                        showTips(Configration.OFF_LINE_TIPS);
                    }
                }
            }
        });
    }

    private void goToCarView() {
        Intent intentGoShopCart = new Intent(NewMainActivity.this, ShoppingCartActivity.class);
        startActivityForResult(intentGoShopCart, CAR_REQUEST_CODE);
//        new RetrofitRequest<>(ApiRequest.getApiShiji().isLogin()).handRequest(new MsgCallBack() {
//            @Override
//            public void onResult(HttpMessage msg) {
//                if (msg.isSuccess()) {
//                    Intent intentGoShopCart = new Intent(NewMainActivity.this, ShoppingCartActivity.class);
//                    startActivityForResult(intentGoShopCart, CAR_REQUEST_CODE);
//                } else if (msg.isLossLogin()) {
//                    Intent intent = new Intent(NewMainActivity.this, LoginActivity.class);
//                    startActivityForResult(intent, CAR_REQUEST_CODE);
//                    overridePendingTransition(R.anim.slide_in_top_buttom, R.anim.slide_out_top_buttom);
//                } else {
//                    if(!NetUtil.IsInNetwork(NewMainActivity.this)){
//                        showTips(Configration.OFF_LINE_TIPS);
//                    }
//                }
//            }
//        });
    }
}
